//
// Ishtar C++ Utility Library
//
// Task Facility - Work Executor Header
//

#ifndef __ISHTAR_TASK_WORK_EXECUTOR_H
#define __ISHTAR_TASK_WORK_EXECUTOR_H

#include <Ishtar/Ishtar.h>

#if defined( ISHTAR_COMPILER_HAS_PRAGMA_ONCE )
#pragma once
#endif

#include <Ishtar/Task/TaskFwd.h>
#include <Ishtar/Task/Work.h>


namespace Ishtar
{

namespace Task
{

///////////////////////////////////////////////////////////////////////////////
//
// Work Executor
//

class WorkExecutor
{
public:

    virtual ~WorkExecutor() {}

    virtual void Shutdown() = 0;

    
    //
    // Submitting Works
    //

    virtual void Submit( const Work& w ) = 0;

    
    /// in-place functions ///

    Work SubmitWork( const std::string& workName, const WorkFunction& workFunc );
    Work SubmitDelayWork( const std::string& workName, const WorkFunction& workFunc, Uint delay );  // *1

    // *1 - delay is in milliseconds

    /// properties about works ///

    virtual Uint GetNumWorks() const = 0;
      // works which are submitted and not completed yet.

    virtual Uint GetNumReadyWorks() const = 0;
      // works which are in the ready queue, ready to run, but not run yet.

};


//
// Implementation
//

inline Work WorkExecutor::SubmitWork( const std::string& workName, const WorkFunction& workFunc )
{
    Work w( workName, workFunc );
    this->Submit( w );
    return w;
}


inline Work WorkExecutor::SubmitDelayWork(
    const std::string& workName, const WorkFunction& workFunc, Uint delay )
{
    Work w( workName, workFunc );
    w.DelayFor( delay );
    this->Submit( w );
    return w;
}


///////////////////////////////////////////////////////////////////////////////

} // namespace Task

} // namespace Ishtar

#endif // __ISHTAR_TASK_WORK_EXECUTOR_H
